﻿package  
{
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.events.IEventDispatcher;
	import flash.utils.Dictionary;
	
	/**
	 * ...
	 * @author kasoi
	 */
	public class StageManager 
	{
		static public function get stageWidth() : Number
		{
			if (StageManager._stage.stageWidth < StageManager._minimalWidth) 
				return StageManager._minimalWidth;
			return StageManager._stage.stageWidth;
		}
		
		static public function get stageHeight() : Number
		{
			if (StageManager._stage.stageHeight < StageManager._minimalHeight) 
				return StageManager._minimalHeight;
			return StageManager._stage.stageHeight;
		}
		
		static public function get mouseX() : Number
		{
			return StageManager._main.mouseX;
		}
		
		static public function get mouseY() : Number
		{
			return StageManager._main.mouseY;
		}
		
		static public function get stage() : Stage
		{
			return StageManager._stage;
		}
		
		static public function get main() : DisplayObjectContainer
		{
			return StageManager._main;
		}
		
		static public function get minimalWidth() : Number 
		{
			return StageManager._minimalWidth;
		}
		
		static public function set minimalWidth(value: Number) : void 
		{
			StageManager._minimalWidth = value;
		}
		
		static public function get minimalHeight() : Number 
		{
			return StageManager._minimalHeight;
		}
		
		static public function set minimalHeight(value: Number) : void 
		{
			StageManager._minimalHeight = value;
		}
		
		
		
		static public function addResizable(method: Function) : void 
		{
			StageManager.resizables.push(method);
			method();
		}
		
		static public function removeResizable(method: Function) : void 
		{
			if (StageManager.resizables.length == 1)
			{
				StageManager.resizables = [];
				return;
			}
			
			for (var i: int = 0; i < StageManager.resizables.length; i++) 
			{
				if (method === StageManager.resizables[i])
				{
					StageManager.resizables.splice(i, 1);
				}
			}
		}
		
		
		static private var resizables: Array;
		
		static private var _stage: Stage;
		
		static private var _main: DisplayObjectContainer;
		
		static private var _switch: Boolean = false;
		
		static private var _minimalWidth: Number = 1;
		
		static private var _minimalHeight: Number = 1;
		
		static public function init(stg: Stage, main: DisplayObjectContainer = null, 
			switchMainWithStage: Boolean = false) : void 
		{
			stg.align = StageAlign.TOP_LEFT;
			stg.scaleMode = StageScaleMode.NO_SCALE;
			StageManager._stage = stg;
			StageManager._main = main;
			StageManager._switch = switchMainWithStage;
			
			if (main == null) StageManager._main = stg;
			
			StageManager.resizables = [];
			
			stg.addEventListener(Event.RESIZE, StageManager.stage_onResize);
		}
		
		static public function resizeAll() : void 
		{
			StageManager.stage_onResize(null);
		}
		
		static private function stage_onResize(e: Event) : void 
		{
			for (var i: int = 0; i < StageManager.resizables.length; i++) 
			{
				var f: Function = StageManager.resizables[i];
				if(f != null) f();
			}
		}
	}
	
}